<template>
	<BaseContainer>
		<aplid-base-crud ref="crud" class="w-100" v-bind="baseConfig" :operateFormate="handleOperate"></aplid-base-crud>

		<aplid-dialog
			@close="onClose"
			width="85%"
			v-model="visibleForm"
			append-to-body
			whickComm="Archives"
			:customeHeader="customeHeader"
			:model="{ ...nowRowData, ...detailHeadData }"
		>
			<transition name="fade" mode="out-in">
				<div class="dialog-content w-100" v-show="!Loading" v-if="visibleForm">
					<oldmanArchives :nowRowData="nowRowData" v-if="nowRowData.tableName == 'dp_service_object'" />
					<orgArchives :nowRowData="nowRowData" v-if="nowRowData.tableName == 'dp_service_org_base'" />
					<practitionerArchives :nowRowData="nowRowData" v-if="nowRowData.tableName == 'dp_foundation_practitioners'" />
				</div>
			</transition>
			<PageLoading v-if="Loading" />
		</aplid-dialog>
	</BaseContainer>
</template>

<script setup>
import apiMod from '@/apiMod/index'
import { baseConfig, selfCustomeHeader } from './config'
import oldmanArchives from '../../basicData/oldman/components/Archives/index.vue'
import orgArchives from '../../basicData/serviceOrg/components/Archives/index.vue'
import practitionerArchives from '../../basicData/practitioner/Archives/index.vue'
import { useCommonStore } from '@/stores/common'

const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()
const { Loading, detailHeadData } = storeToRefs(useCommonStore())

const visibleForm = ref(false)
const customeHeader = ref({})

const nowRowData = ref({})
const onClose = () => {
	setTempParams({})
	setGloablParams({})
	setMoreSearch({})
}

const getServiceOrgInfo = (creditCode) => {
	return new Promise((resolve, reject) => {
		if (!creditCode) {
			reject()
		}
		apiMod.view_dpServiceOrgBase_creditCode(creditCode).then((res) => {
			resolve(res.data)
		})
	})
}

const handleOperate = (row, type) => {
	return new Promise(async (resolve, reject) => {
    if(type =='refresh') {
      resolve()
      return
    }
		nowRowData.value = {
			idCard: row.sourceKey,
			isBlockchain: false,
			tableName: row.tableName,
		}

		switch (row.tableName) {
			case 'dp_service_object':
				customeHeader.value = selfCustomeHeader.oldmanCustomeHeader
				break
			case 'dp_service_org_base':
				customeHeader.value = selfCustomeHeader.orgCustomeHeader
				break
			case 'dp_foundation_practitioners':
				customeHeader.value = selfCustomeHeader.practitionerCustomeHeader
        nowRowData.value.id = row.sourceId
				break
			default:
				break
		}
		if (row.tableName != 'dp_service_org_base') {
			visibleForm.value = true
		} else {
			const data = await getServiceOrgInfo(row.sourceKey)
			nowRowData.value = {
				...data,
				creditCode: row.sourceKey,
				tableName: row.tableName,
			}
			visibleForm.value = true
		}
	})
}
</script>

<style lang="scss" scoped></style>
